package com.coolw.test.validator;

import com.coolw.test.validator.annotation.Password;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 密码校验器
 *
 * @author coolw
 * @date 2025/3/20 15:19
 */
public class PasswordValidator implements ConstraintValidator<Password, String> {

    private int min;
    private int max;

    @Override
    public void initialize(Password constraintAnnotation) {
        min = constraintAnnotation.min();
        max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null) {
            throw new NullPointerException("密码不能为空");
        }

        int length = s.length();
        return length >= min && length <= max;
    }
}
